package itesm.mx;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

//TODO juliana> Cambiar los MAX scores (10) a la clase de quizes por cada quiz

public class HighScores extends Activity {

	public static final String FLAGS_TIMES = "hs_flags_times";
	public static final String FLAGS_WINS = "hs_flags_wins";
	public static final String FLAGS_SCORE = "hs_flags_score";
	public static final String MAPS_TIMES = "hs_maps_times";
	public static final String MAPS_WINS = "hs_maps_wins";
	public static final String MAPS_SCORE = "hs_maps_score";
	public static final String CAPITALS_TIMES = "hs_capitals_times";
	public static final String CAPITALS_WINS = "hs_capitals_wins";
	public static final String CAPITALS_SCORE = "hs_capitals_score";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.high_scores);

		loadStats();

		((Button) findViewById(R.id.btnClearStats)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				clearStats();
				loadStats();
			}
		});
		
		((Button) findViewById(R.id.btnClose)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void loadStats() {
		SharedPreferences prefs = getSharedPreferences(AndroidProject.DROID_TRIVIA_PREFERENCES, Activity.MODE_PRIVATE);

		int flags = prefs.getInt(HighScores.FLAGS_TIMES, 0);
		int maps = prefs.getInt(HighScores.MAPS_TIMES, 0);
		int capitals = prefs.getInt(HighScores.CAPITALS_TIMES, 0);
		int totalTimes = flags + maps + capitals;
		StringBuilder sb = new StringBuilder();
		if (totalTimes != 0) {
			if (flags != 0) {
				int wins = prefs.getInt(HighScores.FLAGS_WINS, 0);
				int score = prefs.getInt(HighScores.FLAGS_SCORE, 0);
				sb.append(getResources().getString(R.string.lblHS2))
						.append(String.format("%.2f", (double)wins / flags * 100))
						.append(getResources().getString(R.string.lblHS3))
						.append(String.format("%.2f", (double)score / flags))
						.append(getResources().getString(R.string.lblHS4))
						.append(flags)
						.append("\n\n");
			}
			if (maps != 0) {
				int wins = prefs.getInt(HighScores.MAPS_WINS, 0);
				int score = prefs.getInt(HighScores.MAPS_SCORE, 0);
				sb.append(getResources().getString(R.string.lblHS5))
						.append(String.format("%.2f", (double)wins / maps * 100))
						.append(getResources().getString(R.string.lblHS3))
						.append(String.format("%.2f", (double)score / maps))
						.append(getResources().getString(R.string.lblHS4))
						.append(maps)
						.append("\n\n");
			}
			if (capitals != 0) {
				int wins = prefs.getInt(HighScores.CAPITALS_WINS, 0);
				int score = prefs.getInt(HighScores.CAPITALS_SCORE, 0);
				sb.append(getResources().getString(R.string.lblHS6))
						.append(String.format("%.2f", (double)wins / capitals * 100))
						.append(getResources().getString(R.string.lblHS3))
						.append(String.format("%.2f", (double)score / capitals))
						.append(getResources().getString(R.string.lblHS4))
						.append(capitals)
						.append("\n\n");
			}
		} else {
			sb.append(getResources().getString(R.string.lblHS1));
		}

		((TextView) findViewById(R.id.tvHighScores)).setText(sb.toString());
	}

	private void clearStats() {
		SharedPreferences prefs = getSharedPreferences(
				AndroidProject.DROID_TRIVIA_PREFERENCES, Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(HighScores.FLAGS_TIMES, 0)
			  .putInt(HighScores.FLAGS_SCORE, 0)
			  .putInt(HighScores.FLAGS_WINS, 0);
		editor.putInt(HighScores.MAPS_TIMES, 0)
			  .putInt(HighScores.MAPS_SCORE, 0)
			  .putInt(HighScores.MAPS_WINS, 0);
		editor.putInt(HighScores.CAPITALS_TIMES, 0)
			  .putInt(HighScores.CAPITALS_SCORE, 0)
			  .putInt(HighScores.CAPITALS_WINS, 0);
		editor.commit();
	}
}
